// pages/my/registered.js
const area_standard = require('../../utils/areaStandard');
var $api = require('../../utils/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_type:1,
    avatar: null,
    username: "", //真实姓名
    mobile: "", //手机号码
    chehang_name: "", //车行名称
    chehang_address: "", //车行地址
    id_img: [], //图片url
    region: "", //区域
    regionOption: false, //弹出层
    regionList: [], //区域选择
    code: "", //区域代码
    isAgree: false, //是否同意协议
    isRegistered: 1,
    is_ver: 1,
    userInfo:[],

    userShow: false,
    privateShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var thatsss = this;
    $api.userInfo().then(res => {
      if (res.code == 1) {
        thatsss.setData({
          userInfo: res.data,
          avatar: res.data.avatarUrl,
          isRegistered: res.data.id_img ? 1 : 0,
          is_ver: res.data.data_verification,
          username: res.data.username,
          mobile:res.data.mobile,
          chehang_name:res.data.chehang_name,
          region:res.data.region,
          chehang_address:res.data.chehang_address,
          id_img: res.data.id_img ? res.data.id_img : [],
          regionList: area_standard.default,
          order_type: options.order_type
        })
      }
    })
  },

  //打开弹出层
  userPop: function () {
    wx.navigateTo({
      url: '../text/userService',
    })
  },
  privatePop: function () {
    wx.navigateTo({
      url: '../text/private',
    })
  },

  //跳转到续费页面
  renew: function () {
    wx.navigateTo({
      url: '../my/myInfo',
    })
  },
  openRegionOption: function (e) {
    if(this.data.is_ver == 1)
    {
      return;
    }
    this.setData({
      regionOption: true
    })
  },
  //关闭弹出层
  onClose: function (e) {
    this.setData({
      regionOption: false
    })
  },
  //选择地区
  chooseRegion: function (e) {
    let list = e.detail.values;
    //拼接后端接受的区域格式
    let region = list[0].name + "/" + list[1].name + "/" + list[2].name;
    this.setData({
      region: region,
      code: list[2].code,
      regionOption: false
    })
  },
  //是否同意协议
  onChangeRadio: function (e) {
    if (!this.data.isAgree) {
      this.setData({
        isAgree: e.detail
      })
    } else {
      this.setData({
        isAgree: false
      })
    }
  },

  //文件上传
  uploadImage: function (path) {
    return new Promise(function (resolve, reject) {
      $api.upLoad(path).then(res => {
        let resData = JSON.parse(res);
        if (resData.code == 1) {
          resolve(resData.data.url);
        }
      })
    })
  },
  afterRead: function (event) {
    const files = event.detail.file;
    console.log(this.data)
    var that = this;
    for (var index in files) {
      this.uploadImage(files[index].path).then(res => {
        let id_img = that.data.id_img;
        id_img.push({
          url: res
        })
        that.setData({
          id_img:id_img
        })
      }).catch(err => { //请求失败
        console.log(err);
      });
    };
  },
  //删除上传
  delete: function (event) {
    if(this.data.is_ver == 1)
    {
      return;
    }
    let imgDelIndex = event.detail.index;
    let id_img = this.data.id_img;
    id_img.splice(imgDelIndex, 1);
    this.setData({
      id_img
    })
  },

  //提交注册
  sumbit: function () {
    if (this.data.id_img != "") {
      // let img_arr = [];
      // for (let i = 0; i < this.data.id_img.length; i++) {
      //   img_arr.push(this.data.id_img[i].url);
      // }
      let img_arr = '';
      for (let i = 0; i < this.data.id_img.length; i++) {
        img_arr += this.data.id_img[i].url + ','
      }
      //去除最后一个逗号
      let reg = /,$/gi;
      img_arr = img_arr.replace(reg, "");

      let params = {
        username: this.data.username,
        mobile: this.data.mobile,
        chehang_name: this.data.chehang_name,
        region: this.data.region,
        chehang_address: this.data.chehang_address,
        id_img: img_arr
      }
      $api.editUserInfo(params).then(res => {
        if (res.code == 1) {
          //跳转到首页
          wx.setStorageSync('isRegistered', true)
          wx.navigateBack({
            delta: 1
          })
        }
      });
    } else {
      wx.showToast({
        title: '请上传相关证件后再次提交',
        icon: 'none',
        duration: 2000 //持续的时间
      })
    }
  },
  //获取授权
  bindgetuserinfo: function (e) {
    var self = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              let params = {
                openid: wx.getStorageSync('openid'),
                rawData: res.rawData
              }
              $api.miniGetuserinfo(params).then(res => {
                  if(res.code==1){
                    self.sumbit();
                  }
              })
            }
          })
        }
      }
    })
    console.log(e);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  
  //电话联系
  contact: function (e) {
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  
  //返回个人中心
  goback: function (e) {
    console.log(e)
    wx.switchTab({
      url: './index',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})